package cn.ctyun.ipush.web.controller;

import cn.ctyun.ipush.web.user.bo.MenuTreeNode;
import cn.ctyun.ipush.web.user.bo.UserModel;
import cn.ctyun.ipush.web.user.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by yanghongbo on 2016/7/11.
 */
@Controller
@RequestMapping
public class MenuController {

    @Resource
    private MenuService menuService;


    /**
     * 用于进入页面后，显示数据
     */
    @RequestMapping(value = "menuIntoPage", method = RequestMethod.GET)
    public ModelAndView intoPage(HttpSession session,HttpServletRequest request){

        //算法详情
        //1、取出session 中的USER 查询dataSource 中的createUser
        //2、判断登录与否
        //3、取出所有用户信息
        //4、
        UserModel systemUserModel = new UserModel();
        try {
            Object o=session.getAttribute("userInfo");
            if(o!=null){

                systemUserModel = (UserModel)o;

                //构建一个Map<level=1,List<level=2>>
                Map<MenuTreeNode,List<MenuTreeNode>> map = new LinkedHashMap<MenuTreeNode, List<MenuTreeNode>>();

                MenuTreeNode menuTreeNode= new MenuTreeNode();
                List<MenuTreeNode> allList = menuService.findAllMenuTree(menuTreeNode);

                List<MenuTreeNode> levelOneList = new ArrayList<MenuTreeNode>();
                List<MenuTreeNode> levelTwoList = new ArrayList<MenuTreeNode>();
                if(allList!=null&&!allList.isEmpty()){

                    for(MenuTreeNode e:allList){
                        if (e.getMenuLevel()==1){
                            levelOneList.add(e);
                        }else {
                            levelTwoList.add(e);
                        }
                    }

                }
                if(!levelOneList.isEmpty()){

                    for(MenuTreeNode e:levelOneList){

                        List<MenuTreeNode> list= new ArrayList<MenuTreeNode>();

                        for(MenuTreeNode son:levelTwoList){
                            if(son.getParentId().equals(e.getMenuTreeNodeId())){
                                list.add(son);
                            }
                        }

                        map.put(e,list);

                    }

                }





                Map<String,Object> data = new HashMap<String,Object>();
                data.put("menuMap",map);
                return new ModelAndView("menuManager",data);
            }else{
                //没有登录。。。。跳回login
                return new ModelAndView("login",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //没有登录。。。。跳回login
            return new ModelAndView("login",null);
        }

    }

    /**
     * 用于进入页面后，显示数据
     */
    @RequestMapping(value = "addMenu", method = RequestMethod.POST)
    public ModelAndView addMenu(HttpSession session,HttpServletRequest request){

        //算法详情
        //1、取出session 中的USER 查询dataSource 中的createUser
        //2、判断登录与否
        //3、取出所有用户信息
        //4、
        UserModel systemUserModel = new UserModel();
        try {
            Object o=session.getAttribute("userInfo");
            if(o!=null){

                systemUserModel = (UserModel)o;

                String menuName =request.getParameter("menuName");
                String menuURL=request.getParameter("menuURL");
                String menuFather=request.getParameter("menuFather");

                MenuTreeNode addMenuTreeNode = new MenuTreeNode();
                addMenuTreeNode.setMenuTreeNodeId(UUID.randomUUID().toString());
                addMenuTreeNode.setMenuLevel(2);
                addMenuTreeNode.setMenuName(menuName);
                addMenuTreeNode.setMenuOrder(4);
                addMenuTreeNode.setUrl(menuURL);
                addMenuTreeNode.setParentId(menuFather);
                Map<String, Object> data = new HashMap<String, Object>();
                if(menuService.addMenu(addMenuTreeNode)) {


                    //构建一个Map<level=1,List<level=2>>
                    Map<MenuTreeNode, List<MenuTreeNode>> map = new LinkedHashMap<MenuTreeNode, List<MenuTreeNode>>();

                    MenuTreeNode menuTreeNode = new MenuTreeNode();
                    List<MenuTreeNode> allList = menuService.findAllMenuTree(menuTreeNode);

                    List<MenuTreeNode> levelOneList = new ArrayList<MenuTreeNode>();
                    List<MenuTreeNode> levelTwoList = new ArrayList<MenuTreeNode>();
                    if (allList != null && !allList.isEmpty()) {

                        for (MenuTreeNode e : allList) {
                            if (e.getMenuLevel() == 1) {
                                levelOneList.add(e);
                            } else {
                                levelTwoList.add(e);
                            }
                        }

                    }
                    if (!levelOneList.isEmpty()) {

                        for (MenuTreeNode e : levelOneList) {

                            List<MenuTreeNode> list = new ArrayList<MenuTreeNode>();

                            for (MenuTreeNode son : levelTwoList) {
                                if (son.getParentId().equals(e.getMenuTreeNodeId())) {
                                    list.add(son);
                                }
                            }

                            map.put(e, list);

                        }

                    }


                    data.put("msg", "添加成功");
                    data.put("menuMap", map);
                }else{
                    //构建一个Map<level=1,List<level=2>>
                    Map<MenuTreeNode, List<MenuTreeNode>> map = new LinkedHashMap<MenuTreeNode, List<MenuTreeNode>>();

                    MenuTreeNode menuTreeNode = new MenuTreeNode();
                    List<MenuTreeNode> allList = menuService.findAllMenuTree(menuTreeNode);

                    List<MenuTreeNode> levelOneList = new ArrayList<MenuTreeNode>();
                    List<MenuTreeNode> levelTwoList = new ArrayList<MenuTreeNode>();
                    if (allList != null && !allList.isEmpty()) {

                        for (MenuTreeNode e : allList) {
                            if (e.getMenuLevel() == 1) {
                                levelOneList.add(e);
                            } else {
                                levelTwoList.add(e);
                            }
                        }

                    }
                    if (!levelOneList.isEmpty()) {

                        for (MenuTreeNode e : levelOneList) {

                            List<MenuTreeNode> list = new ArrayList<MenuTreeNode>();

                            for (MenuTreeNode son : levelTwoList) {
                                if (son.getParentId().equals(e.getMenuTreeNodeId())) {
                                    list.add(son);
                                }
                            }

                            map.put(e, list);

                        }

                    }
                    data.put("menuMap", map);
                    data.put("msg", "添加失败");
                }
                return new ModelAndView("menuManager",data);
            }else{
                //没有登录。。。。跳回login
                return new ModelAndView("login",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //没有登录。。。。跳回login
            return new ModelAndView("login",null);
        }

    }


}
